package com.xm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author：xm
 * @Project：runoob
 * @Date：2024/7/18 15:08
 */

public class URL09 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se");
        URL url222 = new URL("https://www.runoob.com:8089/index.html?language=cn#j2se");

        System.out.println("url 为：" + url);
        System.out.println("url.toString() = " + url.toString());

        System.out.println("协议为：" + url.getProtocol());
        System.out.println("验证信息：" + url.getAuthority());
        System.out.println("222验证信息：" + url222.getAuthority());

        System.out.println("文件名及请求参数：" + url.getFile());
        System.out.println("主机名：" + url.getHost());
        System.out.println("222主机名：" + url222.getHost());

        System.out.println("路径：" + url.getPath());
        System.out.println("端口：" + url.getPort());
        System.out.println("222端口：" + url222.getPort());
        System.out.println("默认端口：" + url.getDefaultPort());
        System.out.println("222默认端口：" + url222.getDefaultPort());

        System.out.println("请求参数：" + url.getQuery());
        System.out.println("定位位置：" + url.getRef());
        System.out.println("222定位位置：" + url222.getRef());

        System.out.println("---华丽的分割线---");

        URL url333 = new URL("https://www.runoob.com");
        URLConnection urlConnection = url333.openConnection();
        HttpURLConnection httpURLConnection = null;
        if (urlConnection instanceof HttpURLConnection) {
            httpURLConnection = (HttpURLConnection) urlConnection;
        } else {
            System.out.println("请输入 URL 地址");
            return;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String urlString="";
        String current;
        while((current = bufferedReader.readLine()) != null) {
            urlString += current;
        }
        System.out.println(urlString);
    }
}
